
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>sleep()和yield()区别 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>sleep()和yield()区别 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">功能</div>
          <div class="card-question">Java中的 `sleep()` 和 `yield()` 方法在核心功能上有何根本区别？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">功能</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`sleep()` 方法使当前线程在指定时间内完全暂停执行，进入休眠状态。而 `yield()` 方法只是向线程调度器发出一个提示，表示当前线程愿意让出CPU资源，但并不保证线程会立即暂停，它是一个建议而非强制命令。</div>
          </div>
          <div class="card-source">来源: 1. 功能</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">当一个Java线程调用 `sleep()` 方法后，它的线程状态会如何变化？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">线程在调用 `sleep()` 后会进入 `TIMED_WAITING` 状态。直到指定的休眠时间结束后，线程才会重新变为 `RUNNABLE` 状态，并等待CPU的调度。</div>
          </div>
          <div class="card-source">来源: 2. 线程状态</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">当一个Java线程调用 `yield()` 方法后，它的线程状态会如何变化？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">线程调用 `yield()` 后会从 `RUNNING` 状态转换回 `READY`（或 `RUNNABLE`）状态。之后，它可能立即被调度器重新选中并执行，也可能等待其他线程执行完毕。</div>
          </div>
          <div class="card-source">来源: 2. 线程状态</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">如果一个线程在持有锁的情况下调用 `sleep()` 或 `yield()`，它持有的锁会被释放吗？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">不会。无论是调用 `sleep()` 还是 `yield()`，线程都不会释放其持有的锁。`sleep()` 在休眠期间会一直持有锁，而 `yield()` 只是暂时让出CPU时间片，同样不影响锁的持有状态。</div>
          </div>
          <div class="card-source">来源: 3. 锁的持有</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">应用</div>
          <div class="card-question">`sleep()` 方法通常用于哪些编程场景？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">应用</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`sleep()` 方法适用于需要实现固定时间延迟的场景，例如实现定时器、执行周期性的轮询任务，或者在某些场景下进行流量控制和限速。</div>
          </div>
          <div class="card-source">来源: 5. 使用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">应用</div>
          <div class="card-question">`yield()` 方法的主要应用目的是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">应用</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`yield()` 方法的主要用途是优化线程调度，通过提示让出CPU资源来减少线程之间的竞争，从而提升系统的整体响应性，尤其适用于多个线程激烈争夺CPU资源的场景。</div>
          </div>
          <div class="card-source">来源: 5. 使用场景</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
